<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - convex geometry</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>
	<body>

		<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - convex geometry</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.module.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { ConvexGeometry } from 'three/addons/geometries/ConvexGeometry.js';
			import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';

			let group, camera, scene, renderer;

			init();

			function init() {

				scene = new THREE.Scene();

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( animate );
				document.body.appendChild( renderer.domElement );

				// camera

				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.set( 15, 20, 30 );
				scene.add( camera );

				// controls

				const controls = new OrbitControls( camera, renderer.domElement );
				controls.minDistance = 20;
				controls.maxDistance = 50;
				controls.maxPolarAngle = Math.PI / 2;

				// ambient light

				scene.add( new THREE.AmbientLight( 0x666666 ) );

				// point light

				const light = new THREE.PointLight( 0xffffff, 3, 0, 0 );
				camera.add( light );

				// helper

				scene.add( new THREE.AxesHelper( 20 ) );

				// textures

				const loader = new THREE.TextureLoader();
				const texture = loader.load( 'textures/sprites/disc.png' );
				texture.colorSpace = THREE.SRGBColorSpace;

				group = new THREE.Group();
				scene.add( group );

				// points

				let dodecahedronGeometry = new THREE.DodecahedronGeometry( 10 );

				// if normal and uv attributes are not removed, mergeVertices() can't consolidate identical vertices with different normal/uv data

				dodecahedronGeometry.deleteAttribute( 'normal' );
				dodecahedronGeometry.deleteAttribute( 'uv' );

				dodecahedronGeometry = BufferGeometryUtils.mergeVertices( dodecahedronGeometry );

				const vertices = [];
				const positionAttribute = dodecahedronGeometry.getAttribute( 'position' );

				for ( let i = 0; i < positionAttribute.count; i ++ ) {

					const vertex = new THREE.Vector3();
					vertex.fromBufferAttribute( positionAttribute, i );
					vertices.push( vertex );

				}

				const pointsMaterial = new THREE.PointsMaterial( {
					color: 0x0080ff,
					map: texture,
					size: 1,
					alphaTest: 0.5
				} );

				const pointsGeometry = new THREE.BufferGeometry().setFromPoints( vertices );

				const points = new THREE.Points( pointsGeometry, pointsMaterial );
				group.add( points );

				// convex hull

				const meshMaterial = new THREE.MeshLambertMaterial( {
					color: 0xffffff,
					opacity: 0.5,
					side: THREE.DoubleSide,
					transparent: true
				} );

				const meshGeometry = new ConvexGeometry( vertices );

				const mesh = new THREE.Mesh( meshGeometry, meshMaterial );
				group.add( mesh );

				//

				window.addEventListener( 'resize', onWindowResize );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				group.rotation.y += 0.005;

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
